/*
 * Copyright 2016 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.channel;

import io.netty.util.IntSupplier;

/**
 * Default select strategy.
 */
final class DefaultSelectStrategy implements SelectStrategy {
    static final SelectStrategy INSTANCE = new DefaultSelectStrategy();

    private DefaultSelectStrategy() { }

    /**
     * 根据指定参数返回一个int值。暂时收集到的使用地点有：
     * 1. NioEventLoop.run()方法中会进行轮询，这里轮询的时候就会通过SelectStrategy返回一个int类型的值。
     *    这里传如的IntSupplier的默认实现就是直接调用selectNow()方法返回的当前可用的事件数，而hasTasks则返回的
     *    是当前taskQueue中是否存在任务。
     *    本方法的作用就是，如果任务队列中有待执行的任务，则首先进行一次selectNow()操作，如果存在客户端事件，
     *    则优先处理客户端事件，否则就处理任务队列中的任务
     */
    @Override
    public int calculateStrategy(IntSupplier selectSupplier, boolean hasTasks) throws Exception {
        return hasTasks ? selectSupplier.get() : SelectStrategy.SELECT;
    }
}
